#ifndef PUZZLEWINDOW_H
#define PUZZLEWINDOW_H

#include <QWidget>
#include <QPushButton>
#include <QGridLayout>
#include<QSoundEffect>
#include <QMainWindow>
#include <QPixmap>
#include <QVector>
#include <QGridLayout>
#include <QShowEvent>
#include "gamecontrolwindow.h"
#include "welcomewindow.h"
#include "recordsmanager.h"
#include<originalimagewindow.h>


class PuzzleWindow : public QWidget
{
    Q_OBJECT

public:
    QString formatTime(int seconds) const;
    PuzzleWindow(int difficulty, WelcomeWindow* welcomeWindow, RecordsManager* recordsManager,
                 QWidget* parent = nullptr);
    void setDifficulty(int newDifficulty);
    void updateBestTimeDisplay();


private slots:
    void handleButtonClick();
    void shuffle();
    bool checkPuzzleSolved();
    void setInteractionEnabled(bool enabled);
    void updatePreviewWindowPosition();
    void toggleOriginalWindowVisibility();


private:
    OriginalImageWindow *m_originalWindow; // 新增原图窗口
    QVector<QString> m_imagePaths;  // 添加图片路径列表
    QString m_currentImagePath;     // 当前选择的图片路径
    bool solved;
    RecordsManager *m_recordsManager;
    QLabel *m_bestTimeLabel;
    WelcomeWindow*welcomeWindow;
    GameControlWindow *controlWindow;
    QVector<QPixmap> imageParts; // 添加图片分片存储
    QPixmap originalImage;       // 原始完整图片
    QSoundEffect soundEffect;
    QSoundEffect succeedSound;
    int m_difficulty;
    QVector<QPushButton*> buttons;
    int emptyRow;
    int emptyCol;
    QGridLayout *gridLayout; // 添加布局成员变量
    void swapButtons(int row1, int col1, int row2, int col2);


signals:
    void destroyed();
    void returnToWelcome();


protected:
    void closeEvent(QCloseEvent *event) override;
    void showEvent(QShowEvent *event) override;
    void moveEvent(QMoveEvent *event)override;
    void resizeEvent(QResizeEvent *event)override;
};

#endif // PUZZLEWINDOW_H

